<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XPKIT Document Generator</title>
<link rel='stylesheet' type='text/css' href='xpkit_doc.css' />

<script language='javascript'>

function load_project_file ()
{
	var name, handle, fso, handle, line, flds, rest, folder;
	
	name = document.project.file.value;
	if (name == "")
	{
		alert ("Please fill in the project file name");
		return;
	}

	fso = new ActiveXObject("Scripting.FileSystemObject");
	if (fso.FileExists(name) == false) 
	{
		alert ("Cannot find the file - " + name);
		return;
	}

	folder = fso.GetParentFolderName(name);
	handle = fso.OpenTextFile(name,1);

	flds = new Array();
	while (!handle.AtEndOfStream)
	{
		line = handle.ReadLine().replace(/^\s+|\s+$/, '');
		if (line.length == 0 || line.charAt(0) == '#') continue;

		flds = line.split (':');
		tmp = flds[0].toLowerCase().replace(/^\s+|\s+$/, '');
		rest = line.substring(tmp.length + 1).replace(/^\s+|\s+$/, '');

		if (tmp == "inputdir")
		{
			if (rest.charAt(0) != "/" && rest.charAt(0) != "\\")
			{
				rest = fso.BuildPath (folder, rest);
			}
			rest = fso.GetAbsolutePathName (rest);
			document.settings.input_dir.value = rest.replace(/\\/g,'/');
		}
		else if (tmp == "outputdir")
		{
			if (rest.charAt(0) != "/" && rest.charAt(0) != "\\")
			{
				rest = fso.BuildPath (folder, rest);
			}
			rest = fso.GetAbsolutePathName (rest);
			document.settings.output_dir.value = rest.replace(/\\/g,'/');
		}
		else if (tmp == "copyright")
		{
			document.settings.copyright.value = rest;
		}
		else if (tmp == "sourceextension")
		{
			document.settings.source_extension.value = rest;
		}
	}

	handle.Close ();
}
</script>

</head>

<body>

<table cellpadding=20 border=0>
<tr><td>

<?php
$cwd = str_replace ('\\', '/', getcwd());
$input_dir = $_GET["input_dir"];
$output_dir = $_GET["output_dir"];
$copyright = $_GET["copyright"];
$source_extension = $_GET["source_extension"];

if ($source_extension == "") $source_extension = ".man";
$source_extensions = explode(" ", $source_extension);
?>

<h2>XPKIT Document Generator</h2>

This page takes advantage of the Script.FileSystemObject ActiveX object to load the project file. The execution of the unsafe ActiveX object should be allowed for it to work properly. The simplest way to do so is to add http://localhost into the trusted sites in Internet Explorer/Tools/Internet Options/Security as this page is supposed to be loaded from the local web server.

<h2></h2>

<form name='project'>
<label>Project File:</label>
<input name="file" type="file" size=50 /> 
<input type="button" value="Load" onClick="javascript:load_project_file()">
</form>

<h2></h2>

<form name='settings' action="<?php print $_SERVER['PHP_SELF']; ?>">
<label>Input Directory:</label>
<input type="text" name="input_dir" value='<?php print $input_dir; ?>' size=50 /><br><br>
<label>Output Directory:</label>
<input type="text" name="output_dir" value='<?php print $output_dir; ?>' size=50 /><br><br>
<label>Copyright:</label>
<input type="text" name="copyright" value='<?php print $copyright; ?>' size=80 /><br><br>
<label>Source Extensions:</label>
<input type="text" name="source_extension" value='<?php print $source_extension; ?>' size=50 /><br><br>
<input type="submit" value="Generate Documents">
</form>

<h2></h2>

<?php
if ($input_dir != "" && $output_dir != "") 
{
	include ("DocFormatter.php");

	if (!is_dir($output_dir) && 
	    mkdir ($output_dir, 0700, true) == false) {
		print "ERROR: Failed to create the directory - $output_dir. Cannot proceed";
	}
	else {
		copy ("$cwd/xpkit_doc.css", "$output_dir/xpkit_doc.css");
		gen_doc ($input_dir, "");
		print "Done generating documents<br>";
	}
}

function gen_doc ($dir, $subdir)
{
	global $source_extensions;

	$handle = opendir ($dir);
	if ($handle == false) return false;

	while (($file = readdir($handle)) != false) {
		if ($file == "CVS" || $file == "." || $file == "..") continue;
		$full = "$dir/$file";
		if (is_dir($full)) {
			if ($subdir == "") $tmp = $file;
			else $tmp = $subdir . "/" . $file;
			gen_doc ($full, $tmp);
		}
		else 
		{
			$len = strlen($file);
			if ($len <= 4) continue;	

			$ext = substr($file,$len-4,4);
			if (in_array ($ext, $source_extensions))
			{
				produce_doc ($full, $subdir, $file);
			}
		}
	}

	closedir ($handle);
}

function produce_doc ($full, $subdir, $file)
{
	global $output_dir;
	global $copyright;

	$formatter = new DocFormatter();	
	$header = $formatter->get_file_title ($full);
	$output = $formatter->format_file ($full);
	if ($output == false) return false;

	if ($subdir == "") $name = $file;
	else $name = "$subdir/$file";
	$name = str_replace ("\\", "-", $name);
	$name = str_replace ("/", "-", $name);
	$name = substr($name, 0, strlen($name) - 4);

	$final = "$output_dir/$name.html";
	
	print "Creating $final...<br>";

	$handle = fopen ($final, "wb");
	if ($handle == false) return false;

	fwrite ($handle, "<html>\n");
	fwrite ($handle, "<head>\n");
	fwrite ($handle, "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />\n");
	fwrite ($handle, "<link rel='stylesheet' type='text/css' href='xpkit_doc.css' />\n");
	fwrite ($handle, "<!--Generated by the XPKIT document generator-->\n");
	fwrite ($handle, "</head>\n");
	fwrite ($handle, "<body>\n");

	fwrite ($handle, "<table border=0>\n");

	if ($header != "")
	{
		fwrite ($handle, "<tr><td>\n");
		fwrite ($handle, $header);
		fwrite ($handle, "<br>");
		fwrite ($handle, "</td></tr>\n");
	}

	fwrite ($handle, "<tr><td>\n");
	fwrite ($handle, $output);
	fwrite ($handle, "</td></tr>\n");

	fwrite ($handle, "<tr><td><div class=footer>\n");

	if ($copyright == "")
	{
		fwrite ($handle, "Generated by XPKIT document generator");
	}
	else
	{
		fwrite ($handle, "$copyright");
	}
	fwrite ($handle, "</div></td></tr>\n");

	fwrite ($handle, "</table>\n");

	fwrite ($handle, "</body>\n");
	fwrite ($handle, "</html>\n");
	fclose ($handle);

	return true;
}
?>

</td></tr>
</table>


</body>
</html>
